// import axios from '@/ajax-mid'
// import $axios from 'axios'
import preloaderFull from '@/modules/preloader-full/preloader-full'
import scriptLoad from '@/modules/corejs/dom/script-load'
import AjaxCache from './ajax-cache.js'

function ajaxCacheGet (axios) {
  const ajaxCache = new AjaxCache(axios)
  function get () {
    return ajaxCache.request()
  }

  return get
}

const api = {
  isCache: true,
  equipmentCode: null,
  getCacheData: ajaxCacheGet(function (succees) {
    // 此处只能执行一次
    return new Promise(function (resolve, reject) {
      preloaderFull.show()
      let timeId
      function clear () {
        clearTimeout(timeId)
      }
      timeId = setTimeout(function () {
        preloaderFull.close()
        clear()
        reject(new Error(`JSONP request to data.json timed out`))
      }, 6000)
      window.jsonp_send = function (data) {
        preloaderFull.close()
        clear()
        resolve(data)
      }
      scriptLoad('data.json')
    }).catch(e => {
      return Promise.reject(e)
    })
  }),
  ajaxGetComm (name) {
    return this.getCacheData().then(data => {
      return data[name]
    })
  },
  getClassRuleEventList () {
    return this.ajaxGetComm('getClassRuleEventList')
  },
  getClassTarget () {
    return this.ajaxGetComm('getClassTarget')
  },
  getClassInfo () {
    return this.ajaxGetComm('getClassInfo')
  },
  getSchoolLogo () {
    return this.ajaxGetComm('getSchoolLogo')
  },
  getDreamHorse () {
    return this.ajaxGetComm('getDreamHorse')
  },
  getBeautifulStars () {
    return this.ajaxGetComm('getBeautifulStars')
  },
  getClassProfile () {
    return this.ajaxGetComm('getClassProfile')
  },
  // 今日签到
  getSignInfo () {
    return this.ajaxGetComm('getSignInfo')
  },
  // 今日表现
  getClassRulePerf () {
    return this.ajaxGetComm('getClassRulePerf')
  },
  // 今日值日
  getDutyInfo () {
    return this.ajaxGetComm('getDutyInfo')
  },
  // 今日明日课表
  getCurriculums () {
    return this.ajaxGetComm('getCurriculums')
  },
  // 班级美少年
  getAdonises () {
    return this.ajaxGetComm('getAdonises')
  },
  // 特色活动、主题班会、安全教育、微阅读
  getASTM () {
    return this.ajaxGetComm('getASTM')
  },

  // ASTM 列表页面
  getListASTM (type) {
    return this.ajaxGetComm('getListASTM').then(data => {
      return data[type]
    })
  },
  // 文章页
  getArticle (type, id) {
    return this.ajaxGetComm('getArticle').then(data => {
      return data[type + id]
    })
  },
  // 缓存所有
  cacheAll () {
  }
}

export default api
